package com.wn.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wn.pojo.Chapter;
import com.wn.mapper.ChapterMapper;
import com.wn.pojo.Video;
import com.wn.service.IChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wn.service.IVideoService;
import com.wn.vo.ChapterVO;
import com.wn.vo.VideoVO;
import jdk.nashorn.internal.runtime.regexp.joni.Warnings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author yellow docker
 * @since 2023-06-21
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements IChapterService {

    @Autowired
    private IVideoService videoService;

    /**
     * 通过 课程 id 查询章节
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVO> listChapterVideoByCourseId(String courseId) {

        //lambda 的查询条件
        LambdaQueryWrapper<Chapter> wrapper = Wrappers.lambdaQuery(Chapter.class).eq(Chapter::getCourseId, courseId);

        //查询章节
        List<Chapter> list = list(wrapper);

        //进行复制
        List<ChapterVO> chapterVOList = BeanUtil.copyToList(list, ChapterVO.class);

        for (ChapterVO chapterVO : chapterVOList) {

            //通过章节的id 查询  视频
            String chapterId = chapterVO.getId();
            LambdaQueryWrapper<Video> qr = Wrappers.lambdaQuery(Video.class).eq(Video::getChapterId, chapterId);
            List<Video> videos = videoService.list(qr);
            //需要吧视频转成 vo
            List<VideoVO> videoVOList = BeanUtil.copyToList(videos, VideoVO.class);

            //排序
            List<VideoVO> collect = videoVOList.stream()         //获取流
                    .sorted(((o1, o2) -> o1.getSort() - o2.getSort())) //中间操作排序 sorted 方法 规则是 sort排序
                    .collect(Collectors.toList());          //终止操作 收集

            chapterVO.setVideoVOList(collect);

        }

        //stream 的操作
//        List<ChapterVO> chapterVOList = list.stream().map(chapter -> {
//            ChapterVO chapterVO = new ChapterVO();
//            return chapterVO;
//        }).collect(Collectors.toList());

        return chapterVOList;
    }

    @Override
    public boolean deleteChapterById(String id) {

//        //查询数据
//        LambdaQueryWrapper<Video> wrapper = Wrappers.lambdaQuery(Video.class);
//        // 通过chapterId 查询下面是否存在 video
//
//        wrapper.eq(Video::getChapterId,id);
//        List<Video> list = videoService.list(wrapper);
//
//        if (CollectionUtils.isEmpty(list)){
//            //为空 删除 chapter
//            boolean remove = removeById(id);
//            return remove;
//        }
//        //判断是否删除
//        // 把 list 所有的都 进行删除
//
//        //删除 chapter 下面的所有视频
//        boolean remove = videoService.removeByIds(list);

        //无论是否 video 先删除
        LambdaQueryWrapper<Video> wrapper = Wrappers.lambdaQuery(Video.class);
        wrapper.eq(Video::getChapterId,id);
        boolean remove = videoService.remove(wrapper);

        //删除 chapter
        remove = removeById(id);

        return remove;
    }
}
